//package com.job.quartz.service.impl;
//
//import com.job.quartz.entity.QrtzIsClustered;
//import com.job.quartz.job.SecondJob;
//import com.job.quartz.service.IQuartzJobService;
//import lombok.extern.slf4j.Slf4j;
//import org.quartz.*;
//import org.quartz.impl.matchers.GroupMatcher;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.util.*;
//
///**
// * @Description Quartz 服务层实现;
// * @Author luolq
// * @Date 2021/9/2 9:20
// * @Vsersion 1.0
// **/
//@Slf4j
//@Service
//public class QuartzJobServiceImpl implements IQuartzJobService {
//
//    @Autowired
//    private Scheduler scheduler;
//
//    @Override
//    public void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String cron, Map<String, Object> optionMap) {
//        try {
//            JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
//            if (null != optionMap) {
//                jobDetail.getJobDataMap().putAll(optionMap);
//            }
//
//            TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
//            triggerBuilder.withIdentity(triggerName, triggerGroupName);
//            triggerBuilder.startNow();
//            triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
//            CronTrigger trigger = (CronTrigger)triggerBuilder.build();
//            Date ft = this.scheduler.scheduleJob(jobDetail, trigger);
//            log.info(jobDetail.getKey() + " has been scheduled to run at: "  + " and repeat based on expression: " + trigger.getCronExpression());
////            boolean flag = scheduler.checkExists(trigger.getKey());
////            if (!flag)
////            {
////                scheduler.start();
////                scheduler.scheduleJob(jobDetail, trigger);
////            }
////            else
////            {
////                //创建一个触发器trigger对象
////                Trigger trigger1 =TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName).withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
////                scheduler.start();
////                scheduler.scheduleJob(jobDetail, trigger1);
////            }
//            if (!this.scheduler.isShutdown()) {
//                this.scheduler.start();
//            }
//        } catch (Exception e) {
//            log.error("创建任务 {} 异常,errorMsg:{}", jobName, e.getCause());
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Override
//    public void addJobTest(String cron) {
//        try {
//
//            JobDetail jobDetail = JobBuilder.newJob(SecondJob.class)
//                    .withIdentity("SUMMERDAY" + " 02")
//                    .storeDurably()
//                    .build();
//            CronScheduleBuilder scheduleBuilder =
//                    CronScheduleBuilder.cronSchedule(cron);
//            // 创建任务触发器
//            Trigger trigger = (Trigger) TriggerBuilder.newTrigger()
//                    .forJob(jobDetail)
//                    .withIdentity("SUMMERDAY" + " 02Trigger")
//                    .withSchedule(scheduleBuilder)
//                    .startNow() //立即執行一次任務
//                    .build();
//            Set<Trigger> set = new HashSet<>();
//            set.add(trigger);
//            // boolean replace 表示启动时对数据库中的quartz的任务进行覆盖。
//            scheduler.scheduleJob(jobDetail, set, true);
//        } catch (Exception e) {
//            log.error("创建任务 {} 异常,errorMsg:{}",  e.getCause());
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Override
//    public void modifyJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName, Class jobClass, String cron, Map<String, Object> optionMap) {
//        log.info("更新定时任务 {} 开始,CronExpression: {}", jobName, cron);
//        try {
//            JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
//            JobDetail jobDetail = this.scheduler.getJobDetail(jobKey);
//            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
//            CronTrigger trigger = (CronTrigger)this.scheduler.getTrigger(triggerKey);
//            if (trigger == null || jobDetail == null) {
//                this.addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron, optionMap);
//                return;
//            }
//
//            this.removeJob(jobName, jobGroupName, triggerName, triggerGroupName);
//            this.addJob(jobName, jobGroupName, triggerName, triggerGroupName, jobClass, cron, optionMap);
//        } catch (Exception e) {
//            log.error("更新定时任务 {} 异常,errorMsg:{}", jobName, e.getCause());
//            throw new RuntimeException(e);
//        }
//        log.info("更新定时任务 {} 结束", jobName);
//    }
//
//    @Override
//    public void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
//        log.info("删除关联任务和触发器{}", jobName);
//        try {
//            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
//            this.scheduler.pauseTrigger(triggerKey);
//            boolean trigger = this.scheduler.unscheduleJob(triggerKey);
//            log.info("trigger删除{}", trigger);
//            boolean job = this.scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));
//            log.info("job删除{}", job);
//        } catch (Exception e) {
//            log.info("删除任务 {} 异常,errorMsg:{}", jobName, e.getCause());
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Override
//    public void pauseJob(String jobName, String jobGroupName) {
//        try {
//            JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
//            this.scheduler.pauseJob(jobKey);
//        } catch (SchedulerException e) {
//            log.info("暂停任务 {} 异常,errorMsg:{}", jobName, e.getCause());
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Override
//    public void resumeJob(String jobName, String jobGroupName) {
//        try {
//            JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
//            this.scheduler.resumeJob(jobKey);
//        } catch (SchedulerException e) {
//            log.info("恢复任务 {} 异常,errorMsg:{}", jobName, e.getCause());
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Override
//    public String getTriggerState(String triggerName, String triggerGroupName) {
//        try {
//            TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
//            Trigger.TriggerState triggerState = this.scheduler.getTriggerState(triggerKey);
//            return triggerState.name();
//        } catch (SchedulerException e) {
//            log.info("获取触发器 {} 异常,errorMsg:{}", triggerName, e.getCause());
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Override
//    public void testQuartz(String jobName, String jobGroupName, String triggerName, String triggerGroupName) throws SchedulerException {
//        JobKey jobKey = JobKey.jobKey(jobName, jobGroupName);
//        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);
//
//        //停止使用相关的触发器
//        //this.scheduler.pauseTrigger(triggerKey);
//        //暂停任务
//        //this.scheduler.pauseJob(jobKey);
//        // 恢复相关的job任务
//        //this.scheduler.resumeJob(jobKey);
//        //回复相关的触发器
//        this.scheduler.resumeTrigger(triggerKey);
//
//
//    }
//}
